package com.smarttest.portal.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ExportExcelControl {

	/**
	 * 描述：通过 jquery.form.js 插件提供的ajax方式导出Excel
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "Message/exportExcel.action", method = {
			RequestMethod.GET, RequestMethod.POST })
	public void exportExcel(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession();
		session.setAttribute("state", null);
		// 生成提示信息，
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("中文", "UTF-8");
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("content-disposition", "attachment;filename="
					+ codedFileName + ".xls");
			// response.addHeader("Content-Disposition",
			// "attachment;   filename=" + codedFileName + ".xls");
			// 产生工作簿对象
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 产生工作表对象
			fOut = response.getOutputStream();
			HSSFSheet sheet = workbook.createSheet();
			HSSFRow rowt = sheet.createRow((int) 0);// 创建一行
			HSSFCell cellt = rowt.createCell((int) 0);// 创建一列
			cellt.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellt.setCellValue("標題");
			
			for (int i = 1; i <= 1000; i++) {
				HSSFRow row = sheet.createRow((int) i);// 创建一行
				HSSFCell cell = row.createCell((int) 0);// 创建一列
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue("测试成功" + i);
				
			}
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
		System.out.println("文件生成...");
	}

	@RequestMapping("check")
	public void check(HttpServletRequest request, HttpServletResponse response) {
		try {
			if ("open".equals(request.getSession().getAttribute("state"))) {
				request.getSession().setAttribute("state", null);
				response.getWriter().write("true");
				response.getWriter().flush();
			} else {
				response.getWriter().write("false");
				response.getWriter().flush();
			}
		} catch (IOException e) {
		}
	}

	@RequestMapping(value = "Message/exportDownload.action", method = {
			RequestMethod.GET, RequestMethod.POST })
	public void exportDownload(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String filename=request.getParameter("filename");
			if (filename == null || "".equals(filename)) {
				filename = "export.text";
			}
			filename = URLDecoder.decode(filename, "UTF-8");
			String suffix = filename.substring(filename.lastIndexOf(".") + 1);

			response.reset();
			response.setCharacterEncoding("GBK");
			response.setHeader("Content-disposition", "attachment;filename="
					+ new String(filename.getBytes("GBK"), "iso8859-1"));
			if (AbstractExport.EXPORT_SUFFIX_ZIP.equals(suffix)) {
				response.setContentType("application/x-zip-compressed");
			} else if (AbstractExport.EXPORT_SUFFIX_EXCEL.equals(suffix)) {
				response.setContentType("application/vnd.ms-excel");
			} else if (AbstractExport.EXPORT_SUFFIX_CSV.equals(suffix)) {
				response.setContentType("application/oct-stream");
			} else if (AbstractExport.EXPORT_SUFFIX_TEXT.equals(suffix)) {
				response.setContentType("text/plain");
			} else if (AbstractExport.EXPORT_SUFFIX_PNG.equals(suffix)) {
				response.setContentType("image/x-png");
			} else {
				response.getWriter().write("Parameter type is not correct!");
				return;
			}
			PrintWriter output = response.getWriter();
			String outStream = "helloword";

			for (int i = 0; i < 1000; i++) {
				output.print("第1列"+i+",");
				output.print("第2列"+i+",");
				output.print("第3列"+i+",");
				output.println();
				if(i %100 ==  1){
					output.flush();
				}
			}
			output.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class AbstractExport {
		static final String EXPORT_SUFFIX_ZIP = "zip";
		static final String EXPORT_SUFFIX_EXCEL = "xls";
		static final String EXPORT_SUFFIX_CSV = "csv";
		static final String EXPORT_SUFFIX_TEXT = "text";
		static final String EXPORT_SUFFIX_PNG = "png";
	}

}
